package cn.itcast.service;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.mapper.SetmealMapper;
import cn.itcast.pojo.Setmeal;
import cn.itcast.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 检查组服务
 */

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {



    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        if(checkgroupIds!=null && checkgroupIds.length >0){
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmealPage = setmealMapper.findPage(queryString);
        long total = setmealPage.getTotal();
        List<Setmeal> result = setmealPage.getResult();
        return new PageResult(total,result);

    }

    @Override
    public void delete(Integer id,String img) {
        setmealMapper.deleteCheckGroupIds(id);
        if (img !=null){
            QiniuUtils.deleteFileFromQiniu(img);
        }
        setmealMapper.deleteSetmeal(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealMapper.findById(id);
        return setmeal;
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        List<Integer> CheckGroupIds =  setmealMapper.findCheckGroupIdsBySetmealId(id);
        return CheckGroupIds;
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //更新基本数据
        setmealMapper.editSetmeal(setmeal);

        //删除原有关系表数据
        setmealMapper.deleteCheckGroupIds(setmeal.getId());

        //添加更改的关系表数据
        setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
    }

    @Override
    public List<Setmeal> getAllSetmeal() {
        return setmealMapper.getAllSetmeal();
    }

    @Override
    public Setmeal findCheckgroupAndCheckitemBySetmealId(Integer id) {
        return setmealMapper.findCheckgroupAndCheckitemBySetmealId(id);
    }

    private void setSetmealAndCheckGroup(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmealId",id);
            map.put("checkgroupId",checkgroupId);
            setmealMapper.setSetmealAndCheckGroup(map);
        }
    }
}
